package com.jenkins.test.system.service.impl;

import com.jenkins.test.base.IBaseServiceImpl;
import com.jenkins.test.system.entity.User;
import com.jenkins.test.system.mapper.UserMapper;
import com.jenkins.test.system.service.UserService;
import com.jenkins.test.web.Person;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: {@link UserServiceImpl}
 * @Description: UserServiceImpl
 * @author: Lanzhou
 * @date: 2020/11/22 17:49
 * @QQ: 1627518680
 * @Copyright:2020 All rights reserved.
 */
@Service
public class UserServiceImpl extends IBaseServiceImpl<UserMapper, User> implements UserService {


    /**
     * 事务测试的结果:
     * 当两个方法都有事务的情况下
     * 1. 被调用方报错(insertForce), 则不会影响调用方(saveTransitional)
     * 2. 调用方报错(saveTransitional), 被调用方(insertForce)会被影响
     *
     * @param person
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveTransitional(Person person) {
        User user = new User();
        user.setId(2L);
        user.setUsername(person.getName());
        user.setNickname(person.getName());
        user.setAge(10);
        user.setEmail("1627518680@QQ.com");
        baseMapper.insert(user);
        insertForce();
    }

    // propagation = Propagation.SUPPORTS,
//    @Transactional(rollbackFor = RuntimeException.class)
    private void insertForce() {
        User user = new User();
        user.setId(3L);
        user.setUsername("MIN-OK");
        user.setNickname("person.getName()");
        user.setAge(125);
        user.setEmail("1627518680@QQ.com");
        baseMapper.insert(user);
        int i = 1 / 0;
    }


}
